'''
调试子界面类， 使用sub_debug.ui生成的布局
手动触发
'''
import enum
import sys
import os
import time
import traceback
from functools import partial

from PyQt5 import QtWidgets

from PyQt5.QtWidgets import QApplication, QHeaderView, QWidget, QMessageBox

from ui.login import Ui_Form  # 界面控件， QtDesigner生成

from log import logger

from plc_S7.HuoniPlcScanClient import Config as plc_config


class LoginWidget(QWidget, Ui_Form):

    def __init__(self, parent=None, subDebugWidget=None):
        super(LoginWidget, self).__init__(parent)

        self.setupUi(self)
        # self.main_ui_handle = main_ui_handle

        self.subDebugWidget = subDebugWidget

        # 额外样式
        self.setStyleSheet("""
                QPushButton {
                    min-width: 80px;
                    padding: 5px;
                    border-radius: 3px;
                    color: white;
                    background-color: dodgerblue;
                }
                QPushButton:hover {
                    background-color: darkblue;
                }

                /* 定义 QGroupBox 样式 */
                QGroupBox {
                    /*font-size: 14px;*/
                    font-weight: bold;
                    border: 1px solid #ccc;
                    border-radius: 8px; /* 使用像素值代替分数 */
                    padding: 1em;
                    margin-top: 1em;
                    background-color: #f5f5f5;
                }

                /* 设置标题样式 */
                QGroupBox::title {
                    subcontrol-origin: margin;
                    /*subcontrol-position: top center;*/
                    padding: 0 1em;
                    color: #333;
                    background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                                    stop: 0 #f5f5f5, stop: 1 #e5e5e5);
                    border-bottom: 1px solid #ccc;
                    border-top-left-radius: 8px;
                    border-top-right-radius: 8px;
                }

                /* 输入框样式 */
                QLineEdit {
                    border: 1px solid #ccc; /* 边框颜色 */
                    border-radius: 4px; /* 边框圆角 */
                    background-color: #fff; /* 背景颜色 */
                }
                /* 输入框焦点样式 */
                QLineEdit:focus {
                    border-color: #80bfff; /* 焦点边框颜色 */
                }
            """)
        self.pushButton.clicked.connect(self.login_solt)  #



    def login_solt(self):
        try:
            print('login_solt')
            input_username = self.LineEdit.text()
            input_password = self.LineEdit_2.text()
            username = 'auto_m'
            password = 'honey123'

            is_ok = input_username == username and input_password == password
            self.LineEdit.setText('')
            self.LineEdit_2.setText('')

            if is_ok:

                self.close()
                self.subDebugWidget.show()

            else:
                qb = QMessageBox()
                qb.setWindowTitle('登录')
                qb.setText('用户名或密码错误')
                qb.exec()


        except:
            print(traceback.format_exc())




if __name__ == '__main__':
    try:
        app = QtWidgets.QApplication(sys.argv)
        mainWnd = LoginWidget()
        # ui = Ui_MainWindow()
        # ui.setupUi(mainWnd)
        mainWnd.show()
        sys.exit(app.exec())
    except:
        print(traceback.format_exc())
        logger.error(traceback.format_exc())